package com.group5.onlinegame.handler;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.group5.onlinegame.entity.Feedback;
import com.group5.onlinegame.entity.FeedbackReturn;
import com.group5.onlinegame.entity.LoginUser;
import com.group5.onlinegame.service.IFeedbackService;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.List;

@Controller
@RequestMapping("/")
public class FeedbackHandler {

    @Resource
    private IFeedbackService feedbackService;

    @RequestMapping("getFeedbacklist")
    @RequiresRoles("system")
    public String getFeedbacklist(String pageNum, Model model, String begin, String end, String typeid, String isreturn) {
        int cpage = 1;
        if (pageNum != null) {
            cpage = Integer.parseInt(pageNum);
        }
        PageHelper.startPage(cpage, 5);
        Feedback feedback = new Feedback();
        feedback.setBegin(begin);
        feedback.setEnd(end);
        Integer typeid_id = null;
        Integer isreturn_id = null;
        try {
            typeid_id = Integer.parseInt(typeid);
        } catch (NumberFormatException e) {
        }
        try {
            isreturn_id = Integer.parseInt(isreturn);
        } catch (NumberFormatException e) {

        }
        feedback.setFeedbacktypeid(typeid_id);
        feedback.setIsreturn(isreturn_id);
        List<Feedback> feedbackList = feedbackService.getFeedback(feedback);
        List<FeedbackReturn> feedbackReturnList = feedbackService.getFeedbackReturn();
        PageInfo<Feedback> feedbackPageInfo = new PageInfo(feedbackList);
        model.addAttribute("feedbackPageInfo", feedbackPageInfo);
        model.addAttribute("feedbackReturnList", feedbackReturnList);
        model.addAttribute("begin", begin);
        model.addAttribute("end", end);
        model.addAttribute("typeid", typeid);
        model.addAttribute("isreturn", isreturn);
        return "feedback";
    }

    @RequestMapping("returnFeedbacklist")
    @RequiresRoles("system")
    public void returnFeedbacklist(String id, String content, HttpSession session, PrintWriter out) {
        FeedbackReturn feedbackReturn = new FeedbackReturn();
        feedbackReturn.setFeedbackid(Integer.parseInt(id));
        feedbackReturn.setReturncontent(content);
        LoginUser loginUser = (LoginUser) session.getAttribute("loginUser");
        feedbackReturn.setReturnuserid(loginUser.getId());
        Integer is = feedbackService.returnFeedback(feedbackReturn);
        feedbackService.changeIsReturn(Integer.parseInt(id));
        out.println(is);
    }

    //创建Excel
    @RequestMapping("createExcel")
    @RequiresRoles("system")
    public void createExcel(HttpServletResponse response, String begin, String end, String typeid, String isreturn) throws IOException {
        //向数据库中查询数据
        Feedback feedback = new Feedback();
        feedback.setBegin(begin);
        feedback.setEnd(end);
        Integer typeid_id = null;
        Integer isreturn_id = null;
        try {
            typeid_id = Integer.parseInt(typeid);
        } catch (NumberFormatException e) {
        }
        try {
            isreturn_id = Integer.parseInt(isreturn);
        } catch (NumberFormatException e) {

        }
        feedback.setFeedbacktypeid(typeid_id);
        feedback.setIsreturn(isreturn_id);
        System.out.println(begin + "," + end + "," + typeid + "," + isreturn);
        List<Feedback> feedbackList = feedbackService.getFeedback(feedback);
        List<FeedbackReturn> feedbackReturnList = feedbackService.getFeedbackReturn();
        //创建HSSFWorkbook对象(excel的文档对象)
        HSSFWorkbook wb = new HSSFWorkbook();
        //建立新的sheet对象（excel的表单）
        HSSFSheet sheet = wb.createSheet("反馈导出报表");
        sheet.setColumnWidth(0,10*256);
        sheet.setColumnWidth(1,25*256);
        sheet.setColumnWidth(2,10*256);
        sheet.setColumnWidth(3,15*256);
        sheet.setColumnWidth(4,10*256);
        sheet.setColumnWidth(5,25*256);
        sheet.setColumnWidth(6,10*256);
        sheet.setColumnWidth(7,15*256);
        // 设置字体
        HSSFFont font = wb.createFont();
        font.setFontHeightInPoints((short) 20); //字体高度
        font.setColor(HSSFFont.COLOR_NORMAL); //字体颜色
        font.setFontName("黑体"); //字体
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //宽度
        // 设置单元格类型1
        HSSFCellStyle cellStyle = wb.createCellStyle();
        cellStyle.setFont(font);
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平布局：居中
        cellStyle.setWrapText(true);
        //在sheet里创建第一行，参数为行索引(excel的行)，可以是0～65535之间的任何一个
        HSSFRow row1 = sheet.createRow(0);
        row1.setHeightInPoints(25);
        //创建单元格（excel的单元格，参数为列索引，可以是0～255之间的任何一个
        HSSFCell cell = row1.createCell(0);
        //设置单元格内容
        cell.setCellValue("反馈导出报表");
        cell.setCellStyle(cellStyle);
        //合并单元格CellRangeAddress构造参数依次表示起始行，截至行，起始列， 截至列
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 7));
        //创建第二行
        HSSFRow row2 = sheet.createRow(1);
        //创建单元格并设置单元格内容
        row2.createCell(0).setCellValue("反馈类型");
        row2.createCell(1).setCellValue("反馈内容");
        row2.createCell(2).setCellValue("反馈用户");
        row2.createCell(3).setCellValue("反馈时间");
        row2.createCell(4).setCellValue("处理状态");
        row2.createCell(5).setCellValue("回复内容");
        row2.createCell(6).setCellValue("回复人");
        row2.createCell(7).setCellValue("处理时间");
        //创建表格内容
        for (int i = 0; i < feedbackList.size(); i++) {
            HSSFRow row3 = sheet.createRow(i + 2);
            Feedback feedback1 = feedbackList.get(i);
            row3.createCell(0).setCellValue(feedback1.getFeedbacktypename());
            row3.createCell(1).setCellValue(feedback1.getContent());
            row3.createCell(2).setCellValue(feedback1.getUsername());
            row3.createCell(3).setCellValue(feedback1.getTime());
            if (feedback1.getIsreturn() == 1) {
                row3.createCell(4).setCellValue("未处理");
                row3.createCell(5).setCellValue("");
                row3.createCell(6).setCellValue("");
                row3.createCell(7).setCellValue("");
            }
            if (feedback1.getIsreturn() == 2) {
                for (int j = 0; j < feedbackReturnList.size(); j++) {
                    FeedbackReturn feedbackReturn = feedbackReturnList.get(j);
                    if (feedbackReturn.getFeedbackid() == feedback1.getId()) {
                        row3.createCell(4).setCellValue("已处理");
                        row3.createCell(5).setCellValue(feedbackReturn.getReturncontent());
                        row3.createCell(6).setCellValue(feedbackReturn.getUsername());
                        row3.createCell(7).setCellValue(feedbackReturn.getReturntime());
                    }
                }
            }
        }
        //输出Excel文件
        OutputStream output = response.getOutputStream();
        response.reset();
        response.setHeader("Content-disposition", "attachment; filename=反馈报表.xls");
        response.setContentType("application/msexcel");
        wb.write(output);
        output.close();
    }
}
